package com.boxflux;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by wangguanglin on 2018/9/14.
 */
@Controller
@RequestMapping("/uas")
public class LoginController {

    @RequestMapping(value = "login",method = RequestMethod.GET)
    public String toLogin(String appType, String service, HttpServletRequest request){
        String TGC = CookieUtils.getCookie(request,"castgc");
        //验证cookies是否失效
        if("TGC-123456".equals(TGC)){
            String ST = "ST-234567";

            return "redirect:"+service+"?token="+ST;
        }
        return appType+"/login";
    }

    @RequestMapping(value = "login",method = RequestMethod.POST)
    public String login(String username, String password, String service, HttpServletRequest request, HttpServletResponse response){
        String ST=null;
        if(StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)){
            //登陆成功
            //生成TGC
            String TGC = "TGC-123456";
            ST = "ST-234567";
            CookieUtils.writeCookie(response,"castgc",TGC);
        }
        return "redirect:"+service+"?token="+ST;
    }
    @RequestMapping(value = "/serviceValidate",method = RequestMethod.GET)
    public String loginSuccess(String service, String token,HttpServletRequest request, HttpServletResponse response){
        String TGC = CookieUtils.getCookie(request,"castgc");
        if("TGC-123456".equals(TGC)){
            String ST = "ST-234567";

            return "redirect:"+service+"?token="+ST;
        }
        return null;
    }
}
